# encoding = utf-8

from playwright.sync_api import sync_playwright

nodeService = None
browser = None
context = None

def openBrowser():
    global nodeService, browser, context
    try:
        nodeService = sync_playwright().start()
        browser = nodeService.chromium.launch(headless=False)
        # 创建 BrowserContext对象
        context = browser.new_context()
        # 启动跟踪功能
        # context.tracing.start(snapshots=True, sources=True, screenshots=True)
    except Exception as err:
        raise err


def visitUrl(url, *arg):
    global context
    try:
        page = context.new_page()
        page.goto(url)
        # page.wait_for_timeout(10000)
    except Exception as err:
        raise err

def closeBroser(*args):
    global browser
    try:
        browser.close()
    except Exception as err:
        raise err

def closeNodeService(*args):
    global nodeService
    try:
        nodeService.stop()
    except Exception as err:
        raise err